<template>
  <div>
    <!--引用表格-->
   <BasicTable @register="registerTable" :rowSelection="rowSelection">
       <!--操作栏-->
      <template #action="{ record }">
        <TableAction :actions="handleTask(record)"/>
      </template>
      <!--字段回显插槽-->
      <template v-slot:bodyCell="{ column, record, index, text }">
      </template>
    </BasicTable>
    <!-- 表单区域 -->
    <TobedoneModal @register="registerModal" @success="handleSuccess"></TobedoneModal>
  </div>
</template>

<script lang="ts" name="tobedone-tobedone" setup>
  import {ref, reactive, computed, unref} from 'vue';
  import {BasicTable, useTable, TableAction} from '/@/components/Table';
  import {useModal} from '/@/components/Modal';
  import { useListPage } from '/@/hooks/system/useListPage'
  import TobedoneModal from './components/TobedoneModal.vue'
  import {columns, searchFormSchema, superQuerySchema} from './Tobedone.data';
  import {list, deleteOne, batchDelete, getImportUrl, getExportUrl, receiveTask, refuseTask} from './Tobedone.api';
  import { downloadFile } from '/@/utils/common/renderUtils';
  import { useUserStore } from '/@/store/modules/user';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { getDateByPicker } from '/@/utils';
  //日期个性化选择
  const fieldPickers = reactive({
  });
  const queryParam = reactive<any>({});
  const checkedKeys = ref<Array<string | number>>([]);
  const userStore = useUserStore();
  const { createMessage } = useMessage();
  //注册model
  const [registerModal, {openModal}] = useModal();
  //注册table数据
  const { prefixCls,tableContext,onExportXls,onImportXls } = useListPage({
      tableProps:{
           title: '待办',
           api: list,
           columns,
           canResize:true,
           formConfig: {
              //labelWidth: 120,
              schemas: searchFormSchema,
              autoSubmitOnEnter:true,
              showAdvancedButton:true,
              fieldMapToNumber: [
              ],
              fieldMapToTime: [
              ],
            },
           actionColumn: {
               width: 150,
               fixed:'right'
            },
            beforeFetch: (params) => {
              if (params && fieldPickers) {
                for (let key in fieldPickers) {
                  if (params[key]) {
                    params[key] = getDateByPicker(params[key], fieldPickers[key]);
                  }
                }
              }
              return Object.assign(params, queryParam);
            },
      },
       exportConfig: {
            name:"待办",
            url: getExportUrl,
            params: queryParam,
          },
          importConfig: {
            url: getImportUrl,
            success: handleSuccess
          },
  })

  const [registerTable, {reload},{ rowSelection, selectedRowKeys }] = tableContext

  // 高级查询配置
  const superQueryConfig = reactive(superQuerySchema);

  /**
   * 高级查询事件
   */
  function handleSuperQuery(params) {
    Object.keys(params).map((k) => {
      queryParam[k] = params[k];
    });
    reload();
  }
   /**
    * 新增事件
    */
  function handleAdd() {
     openModal(true, {
       isUpdate: false,
       showFooter: true,
     });
  }
   /**
    * 批量删除事件
    */
  async function batchHandleDelete() {
     await batchDelete({ids: selectedRowKeys.value}, handleSuccess);
   }
   /**
    * 成功回调
    */
  function handleSuccess() {
      (selectedRowKeys.value = []) && reload();
   }
   /*
   * 接受任务操作栏，驳回任务操作栏
   * */

   function handleTask(record){
    return[
      {
        label:'接收任务',
        auth: 'tobedone:yx_tobedone:receiveTask',
        popConfirm: {
          title: '确定接收此任务吗？',
          confirm: handleReceiveTask.bind(null, record),
          placement: 'topLeft',
        },
      },{
      label: '驳回任务',
        auth: 'tobedone:yx_tobedone:refuseTask',
        popConfirm: {
          title: '确定驳回此任务吗？',
          confirm: handleRefuseTask.bind(null, record),
          placement: 'topLeft',
        },
      }
    ]
   }

   /*
   * 接收任务事件
   * */
  async function handleReceiveTask(record) {
    await receiveTask({id: record.id}, handleSuccess);
  }
  /*
  * 驳回任务事件
  * */
  async function handleRefuseTask(record) {
    await refuseTask({id: record.id}, handleSuccess);
  }




</script>

<style lang="less" scoped>
  :deep(.ant-picker),:deep(.ant-input-number){
    width: 100%;
  }
</style>
